<template>
  <el-dialog :append-to-body="true" :close-on-click-modal="false" :before-close="cancel" :visible.sync="dialog" :title="isAdd ? '新增' : '编辑'" width="500px">
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="120px">
      <el-form-item label="排序">
        <el-input v-model="form.sort" style="width: 300px;" />
      </el-form-item>
      <el-form-item label="标题">
        <el-input v-model="form.name" style="width: 300px;" />
      </el-form-item>
      <!-- <el-form-item label="跳转url">
        <el-input v-model="form.url" style="width: 300px;" />
      </el-form-item> -->
      <el-form-item label="页面">
        <el-radio v-for="item in dict.pages_dict" :key="item.id" v-model="form.path" :label="item.value" @change="chageType(item)">{{ item.label }}</el-radio>
      </el-form-item>
      <el-form-item label="小程序路径">
        <el-col :span="18">{{ form.path }} <el-input v-if="form.path && form.path.lastIndexOf('=') >= 0" v-model="form.queryVal" style="width: 80px;" placeholder="ID" />  </el-col>
        <el-col :span="6"><el-button v-if="paramType === 'goods'" icon="el-icon-s-grid" @click="openGoods('goodsList')" /></el-col>
      </el-form-item>
      <el-form-item label="图片(750*480)">
        <MaterialList v-model="form.imageArr" style="width: 300px" type="image" :num="1" :width="150" :height="150" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <!--<el-input v-model="form.groupName" />-->
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>

    <choosegoods v-if="showGoods" is-sigle @handleclose="cancelGoods" @confirmGoods="confirmGoods" />

  </el-dialog>
</template>

<script>
import { add, edit } from '@/api/yxSystemGroupData'
import picUpload from '@/components/pic-upload'
import MaterialList from '@/components/material'
import choosegoods from '../../components/ChooseGoods'

export default {
  // eslint-disable-next-line vue/no-unused-components
  components: { picUpload, MaterialList, choosegoods },
  dicts: ['pages_dict'],
  props: {
    isAdd: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      loading: false, dialog: false,
      form: {
        id: '',
        groupName: 'routine_home_banner',
        name: '',
        // url: '',
        wxapp_url: '',
        pic: '',
        imageArr: [],
        sort: 0,
        status: '',
        path: '',
        queryVal: ''
      },
      paramType: '',
      rules: {
      },
      showGoods: false
    }
  },
  watch: {
    'form.imageArr': function(val) {
      if (val) {
        this.form.pic = val.join(',')
      }
    }
  },
  methods: {
    chageType(item) {
      this.paramType = item.code
    },
    cancelGoods() {
      this.showGoods = false
    },
    openGoods(val) {
      this.curListName = val
      this.showGoods = true
    },
    confirmGoods(goodsList = []) {
      this.showGoods = !this.showGoods
      if (goodsList && goodsList.length > 0) {
        const goods = goodsList[0]
        console.log('goods===>', goods.storeName, goods, this.form)
        this.form.imageArr = []
        this.form.queryVal = goods.id
        this.form.imageArr.push(goods.image)
        if (!this.form.name) {
          console.log('confirmGoods==>', goods.storeName)
          this.form.name = goods.storeName
        }
        // this.form.image = []
        // if (this.form[this.curListName].length === 0) {
        // this.form[this.curListName] = goodsList
        // } else {
        // const newList = new Set([...goodsList, ...this.form[this.curListName]])
        // this.form[this.curListName] = this.duplicate(Array.from(newList), 'id')
        // }
      }
    },
    cancel() {
      this.resetForm()
    },
    doSubmit() {
      this.loading = true
      if (this.isAdd) {
        this.doAdd()
      } else this.doEdit()
    },
    doAdd() {
      this.form.wxapp_url = this.form.path
      if (this.form.queryVal && this.form.queryVal !== undefined) {
        this.form.wxapp_url = this.form.path + this.form.queryVal
      }
      add(this.form).then(res => {
        this.resetForm()
        this.$notify({
          title: '添加成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.$parent.init()
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
    },
    doEdit() {
      this.form.wxapp_url = this.form.path
      if (this.form.queryVal && this.form.queryVal !== undefined) {
        this.form.wxapp_url = this.form.path + this.form.queryVal
      }
      edit(this.form).then(res => {
        this.resetForm()
        this.$notify({
          title: '修改成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.$parent.init()
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
      this.form = {
        id: '',
        groupName: 'routine_home_banner',
        imageArr: [],
        value: '',
        addTime: '',
        sort: 0,
        status: '',
        name: ''
      }
    }
  }
}
</script>

<style scoped>

</style>
